package com.jstyle.jstylecommon.filter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

import com.jstyle.jstylecommon.R;
import com.jstyle.jstylecommon.R2;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2018/3/28.
 */

public class FilterDialog extends AlertDialog {
    int baseRssi = -40;
    @BindView(R2.id.et_name_filter)
    EditText etFilter;
    @BindView(R2.id.seekBar_deviceRssi)
    SeekBar seekBarRssi;
    @BindView(R2.id.tv_device_dbm)
    TextView tvDbm;
    private DeviceFilterListener deviceFilterListener;

    public FilterDialog(@NonNull Context context, DeviceFilterListener filterListener) {
        super(context);
        this.deviceFilterListener = filterListener;

    }

    protected FilterDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected FilterDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_device_filter);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        etFilter.setFocusable(true);
        etFilter.setFocusableInTouchMode(true);
        etFilter.requestFocus();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        seekBarRssi.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int rssi = baseRssi - progress;
                tvDbm.setText(String.format(getContext().getString(R.string.progress_rssi), rssi));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @OnClick(R2.id.bt_filterName_ok)
    public void onViewClicked() {
        int filterRssi = baseRssi - seekBarRssi.getProgress();
        String filterName = etFilter.getText().toString();
        if (deviceFilterListener != null) deviceFilterListener.DeviceFilter(filterName, filterRssi);
        dismiss();
    }


    public void setFilterName(String name){
        if(!TextUtils.isEmpty(name)&&isShowing())
            etFilter.setText(name);
    }

    public interface DeviceFilterListener {
        public void DeviceFilter(String name, int rssi);
    }
}
